package com.tanhua.dubbo.api.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.ObjectUtil;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.api.UsersApi;
import com.tanhua.dubbo.pojo.Users;
import com.tanhua.dubbo.vo.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@DubboService(version = "1.0.0")
public class UsersApiImpl implements UsersApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveUsers(Long userId, Long friendId) {
        //校验
        if (!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return null;
        }

        //判断好友关系是否存在
        Query query = Query.query(Criteria.where("userId")
                .is(userId).and("friendId").is(friendId));
        long count = this.mongoTemplate.count(query, Users.class);
        if (count > 0) {
            //好友关系已经存在
            return null;
        }

        //保存我与朋友的好友关系
        Users users = new Users();
        users.setUserId(userId);
        users.setFriendId(friendId);
        users.setId(ObjectId.get());
        users.setDate(Convert.toLong(new Date()));
        this.mongoTemplate.save(users);

        //保存朋友与我的好友关系
        users = new Users();
        users.setUserId(friendId);
        users.setFriendId(userId);
        users.setId(ObjectId.get());
        users.setDate(Convert.toLong(new Date()));
        this.mongoTemplate.save(users);

        return users.getId().toHexString();
    }

    @Override
    public Boolean removeUsers(Long userId, Long friendId) {
        if(!ObjectUtil.isAllNotEmpty(userId, friendId)) {
            return false;
        }

        //删除我与好友的关系
        Query query1 = Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        DeleteResult result1 = this.mongoTemplate.remove(query1, Users.class);

        //删除好友与我的关系
        Query query2 = Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId));
        DeleteResult result2 = this.mongoTemplate.remove(query2, Users.class);
        return result1.getDeletedCount() == 1 && result2.getDeletedCount() == 1;
    }

    @Override
    public List<Users> queryAllUsersList(Long userId) {

        Query query = Query.query(Criteria.where("userId").is(userId));

        return this.mongoTemplate.find(query, Users.class);
    }

    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);

        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);
        List<Users> usersList = this.mongoTemplate.find(query, Users.class);

        pageInfo.setRecords(usersList);
        return pageInfo;
    }
}
